<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>岗位详情</title>
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/Sys/js/xadmin.js"></script>
    <style>
        .detail-container {
            padding: 20px;
        }
        .detail-item {
            margin-bottom: 15px;
            display: flex;
        }
        .detail-label {
            width: 100px;
            text-align: right;
            margin-right: 10px;
            color: #666;
        }
        .detail-content {
            flex: 1;
            color: #333;
        }
        .reject-reason {
            color: #ff5722;
        }
        .button-container {
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="detail-container">
    <div class="detail-item">
        <div class="detail-label">科室名称：</div>
        <div class="detail-content" id="departmentName"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">科室地点：</div>
        <div class="detail-content" id="location"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">工作性质：</div>
        <div class="detail-content" id="jobType"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">负责人：</div>
        <div class="detail-content" id="supervisor"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">联系电话：</div>
        <div class="detail-content" id="contactPhone"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">工作时间（工作日）：</div>
        <div class="detail-content" id="workingHours"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">薪酬：</div>
        <div class="detail-content" id="salary"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">所需人数：</div>
        <div class="detail-content" id="headcount"></div>
    </div>
    <div class="detail-item">
        <div class="detail-label">申请时间：</div>
        <div class="detail-content" id="applyTime"></div>
    </div>
<!--    <div class="detail-item">-->
<!--        <div class="detail-label">当前状态：</div>-->
<!--        <div class="detail-content" id="status"></div>-->
<!--    </div>-->
    <div class="detail-item" id="rejectReasonDiv" style="display:none;">
        <div class="detail-label">驳回原因：</div>
        <div class="detail-content reject-reason" id="rejectReason"></div>
    </div>

    <!-- 添加投递按钮容器 -->
    <div class="button-container" id="applyButtonContainer" style="display:none;">
        <button class="layui-btn" onclick="applyJob()">投递申请</button>
    </div>
</div>

<script>
layui.use(['layer'], function(){
    var layer = layui.layer;
    
    // 获取URL中的岗位ID
    var id = getUrlParam('id');
    var userRole = '[[${session.role}]]';
    var userId = '[[${session.id}]]';

    // 如果是学生角色，检查是否已申请过
    if(userRole === 'STUDENT' || userRole === 'student') {
        checkJobApplication(id);
    }
    
    // 加载岗位详情
    $.ajax({
        url: '/Sys/job/detail/' + id,
        type: 'GET',
        success: function(res){
            if(res.code === 0){
                var data = res.data;
                $('#departmentName').text(data.departmentName);
                $('#location').text(data.location);
                $('#jobType').text(data.jobType);
                $('#supervisor').text(data.supervisor);
                $('#contactPhone').text(data.contactPhone);
                $('#workingHours').text(data.workingHours);
                $('#salary').text(data.salary);
                $('#headcount').text(data.headcount);
                $('#applyTime').text(formatDateTime(data.applyTime));
                $('#status').text(data.status);
                
                // 如果状态是已驳回，显示驳回原因
                if(data.status === '已驳回' && data.rejectReason){
                    $('#rejectReasonDiv').show();
                    $('#rejectReason').text(data.rejectReason);
                }
            }else{
                layer.msg('获取岗位详情失败');
            }
        }
    });
});

// 检查是否已申请过该岗位
function checkJobApplication(jobPositionId) {
    $.ajax({
        url: '/Sys/getStudentJob',
        type: 'GET',
        data: {
            userId: '[[${session.id}]]',
            jobPositionId: jobPositionId
        },
        success: function(res) {
            if (!res) {
                // 如果没有申请记录，显示投递按钮
                $('#applyButtonContainer').show();
            }
        }
    });
}

// 投递岗位申请
function applyJob() {
    var jobPositionId = getUrlParam('id');
    
    $.ajax({
        url: '/Sys/addStudentJob',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            userId: '[[${session.id}]]',
            jobPositionId: jobPositionId
        }),
        success: function(res) {
            if(res === "success") {
                layer.msg('申请成功', {
                    icon: 1,
                    time: 2000
                }, function(){
                    // 关闭当前窗口
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    // 刷新父页面
                    parent.location.reload();
                });
            } else {
                layer.msg('申请失败，请重试');
            }
        },
        error: function() {
            layer.msg('服务器错误，请联系管理员');
        }
    });
}

// 获取URL参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}

// 格式化日期时间
function formatDateTime(timestamp) {
    if(!timestamp) return '';
    var date = new Date(timestamp);
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString().padStart(2, '0');
    var day = date.getDate().toString().padStart(2, '0');
    var hours = date.getHours().toString().padStart(2, '0');
    var minutes = date.getMinutes().toString().padStart(2, '0');
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
}
</script>
</body>
</html> 